<?php
/**
 * 广告控制器
 * Date: 2025/09/10
 */

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Admin\AdvertService;
use Illuminate\Http\Request;

class AdvertController extends BaseController
{
    /**
     * 广告列表
     * @date 2025/09/16
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function list(Request $request)
    {
        $params = $this->validate($request, [
            'name' => 'sometimes',
            'type' => 'sometimes',
            'status' => 'sometimes',
            'page' => 'sometimes',
            'size' => 'sometimes'
        ]);
        return (new AdvertService())->list($params);
    }

    /**
     * 广告创建
     * @date 2025/09/16
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function create(Request $request)
    {
        $params = $this->validate($request, [
            'name' => 'required',
            'type' => 'required|in:1,2',
            'jump' => 'required|in:1,2',
            'jump_link' => 'required',
            'sort' => 'required',
            'status' => 'required|in:1,2',
        ], [
            'name.required' => '名称不能为空',
            'type.required' => '类型不能为空',
            'type.in' => '类型必须为菜单或者公告',
            'jump.required' => '跳转方式不能为空',
            'jump.in' => '跳转方式必须为当前窗口或者新窗口',
            'jump_link.required' => '跳转地址不能为空',
            'sort.required' => '排序不能为空',
            'status.required' => '状态不能为空',
            'status.in' => '状态必须为开启或者禁用'
        ]);
        return (new AdvertService())->create($params);
    }

    /**
     * 广告详情
     * @date 2025/09/16
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function detail(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed('广告ID不能为空');
        }
        return (new AdvertService())->detail($id);
    }

    /**
     * 广告更新
     * @date 2025/09/16
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function update(Request $request)
    {
        $params = $this->validate($request, [
            'id' => 'required|integer',
            'name' => 'required',
            'type' => 'required|in:1,2',
            'jump' => 'required|in:1,2',
            'jump_link' => 'required',
            'sort' => 'required',
            'status' => 'required|in:1,2',
        ], [
            'id.required' => '广告ID不能为空',
            'id.integer' => '广告ID必须为数字类型',
            'name.required' => '名称不能为空',
            'type.required' => '类型不能为空',
            'type.in' => '类型必须为菜单或者公告',
            'jump.required' => '跳转方式不能为空',
            'jump.in' => '跳转方式必须为当前窗口或者新窗口',
            'jump_link.required' => '跳转地址不能为空',
            'sort.required' => '排序不能为空',
            'status.required' => '状态不能为空',
            'status.in' => '状态必须为开启或者禁用'
        ]);
        return (new AdvertService())->update($params);
    }

    /**
     * 广告删除
     * @date 2025/09/16
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delete(Request $request)
    {
        $id = $request->input('id', 0);
        if (empty($id)) {
            return $this->failed('广告ID不能为空');
        }
        return (new AdvertService())->delete($id);
    }
}
